package com.example.democourse.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.democourse.dao.CourseTypeDao;
import com.example.democourse.entity.Course;
import com.example.democourse.entity.CourseType;
import com.example.democourse.service.CourseService;
import com.example.democourse.service.CourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private CourseTypeService courseTypeService;

    @Autowired
    private CourseService courseService;
    @RequestMapping("/index")
    //课程名称，标题，课程类型，上下架状态
    public String index(@RequestParam(required = false) String keyword,
                        @RequestParam(defaultValue = "1")Long pageIndex,
                                @RequestParam(defaultValue = "3")Long pageSize,
                                @RequestParam(required = false)Integer status,
                                @RequestParam(required = false)Long courseTypeId,
                                Model model){
        List<CourseType> courseTypeList = courseTypeService.list();
        model.addAttribute("typeList",courseTypeList);
        Page<Course> page = new Page<Course>(pageIndex,pageSize);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (keyword!=null&&!keyword.equals("")){
            QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
            courseQueryWrapper.like("course_name",keyword);
            List<Course> courses =courseService.getBaseMapper().selectList(courseQueryWrapper);
            if (courses.size()>0){
                queryWrapper.like("course_name",keyword);
            }else{
                queryWrapper.like("course_title",keyword);
            }
            model.addAttribute("keyword",keyword);
        }
        if (status!=null&&status!=0){
            queryWrapper.eq("status",status);
            model.addAttribute("status",status);
        }
        if (courseTypeId!=null&&courseTypeId!=0){
            queryWrapper.eq("course_type_id",courseTypeId);
            model.addAttribute("typeId",courseTypeId);
        }
        IPage<Course> iPage = courseService.getBaseMapper().selectPage(page,queryWrapper);
        List<Course> courseList = iPage.getRecords();
        for (Course course : courseList) {
            CourseType courseType = (courseTypeService.getOne(new QueryWrapper<CourseType>().eq("id",course.getCourseTypeId())));
            course.setCourseType(courseType);
            //通过util下的Date包实现
//            Date date = course.getCreateDate();
//            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//            course.setCreateDate(dateFormat.format(date));
//            System.out.println(dateFormat.format(date));
        }
        model.addAttribute("pageList",courseList);
        model.addAttribute("nowPage",iPage.getCurrent());
        Long total = iPage.getTotal()%iPage.getSize()==0?iPage.getTotal()/iPage.getSize():iPage.getTotal()/iPage.getSize()+1;
        model.addAttribute("total",total);
        return "index";
    }

    @GetMapping("/upDown")
    @ResponseBody
    public String upDown(Long id){
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("id",id);
        Course course = courseService.getOne(courseQueryWrapper);
        if (course.getStatus()==1){
            course.setStatus(2);
        }else{
            course.setStatus(1);
        }
        course.setUpDownDate(new Date());
        courseService.updateById(course);
        return "SUCCESS";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete (Long id){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Course course = courseService.getOne(queryWrapper);
        return "SUCCESS";
    }
}
